// SSCTest.cpp : Defines the entry point for the console application.
//

#include <stdio.h>

#include "rsaeuro.h"
#include "rsa.h"
/*
int main(int argc, char* argv[])
{
	printf("Hello World!\n");
	return 0;
}s
*/

//extern  "C"  int   __cdecl   rsapublicfunc PROTO_LIST((unsigned char *, unsigned int *, unsigned char *, unsigned int, R_RSA_PUBLIC_KEY *));

//int __cdecl RSAPublicEnc(unsigned char *,unsigned int *,unsigned char *,unsigned int,struct R_RSA_PUBLIC_KEY *);

//static R_RANDOM_STRUCT *InitRandomStruct(void);
//void initPub(R_RSA_PUBLIC_KEY *rsapub,char *pube,char *pubm);
//void initPub1(R_RSA_PUBLIC_KEY *rsapub);
//void Hex_to_ASCII( char *asc_buf, char *hex_buf, int len );
//void ASCII_to_Hex( char *hex_buf, char *asc_buf, int len );
//void trimoff(char **in);
/*
int main(int argc, char* argv[])
{
	R_RSA_PUBLIC_KEY rsapub;

	R_RANDOM_STRUCT *randomStruct;
	unsigned char output[2048];         
	unsigned int  outputLen;        
	unsigned char input[2048];          
	unsigned int inputLen;         
	unsigned char output1[2048];         
	unsigned int  outputLen1;       
	char buf[2048];
	char *p;
	randomStruct = InitRandomStruct();
	initPub1(&rsapub);
	memset(output,0,sizeof(output));
	strcpy(buf,"3036313233343536");
	//strcpy((char*)input,"06123456FFFFFFFF");
	ASCII_to_Hex((char*)input,buf,strlen(buf));
	inputLen = strlen(buf)/2;
	printf("Hello World!\n");
	RSAPublicEnc(output, &outputLen, input, inputLen, &rsapub);
	memset(buf,0,sizeof(buf));
	Hex_to_ASCII(buf,(char*)output,2*outputLen);
	printf("buf=[%s]\n",buf);
	printf("outputLen=[%d]\n",outputLen);
	initPub1(&rsapub);

	RSAPublicEnc(output1, &outputLen1, output, outputLen, &rsapub);
	memset(buf,0,sizeof(buf));
	Hex_to_ASCII(buf,(char*)output1,2*outputLen1);
	printf("buf=[%s]\n",buf);
	p = buf;
	trimoff(&p);
	printf("buf=[%s]\n",p);
	return 0;
}
 
int enc(char *pube,char *pubm,char *inbuf,int inlen,char *outbuf,int *outlen)
{
	R_RSA_PUBLIC_KEY rsapub;

	unsigned char output[2048];         
	unsigned int  outputLen;        
	unsigned char input[2048];          
	unsigned int inputLen;         
	unsigned char output1[2048];         
	unsigned int  outputLen1;       
	char buf[2048];
	char *p;
	int len;

	initPub(&rsapub,pube,pubm);
	memset(output,0,sizeof(output));

	RSAPublicEnc(output, &outputLen, (unsigned char*)inbuf, inlen, &rsapub);
	Hex_to_ASCII(outbuf,(char*)output,2*outputLen);
	*outlen = 2*outputLen;
    return 0;

}
*/
/*
int md5(char *input,int inlen,char *output,int *outlen)
{
	R_DIGEST_CTX  md5ctxt;
	R_DigestInit(&md5ctxt, DA_MD5);
	R_DigestUpdate(&md5ctxt,(unsigned char*)input, inlen);
	R_DigestFinal(&md5ctxt,(unsigned char*)output,(unsigned int *)outlen);
	return 0;
}

static R_RANDOM_STRUCT *InitRandomStruct(void)
{
	static unsigned char seedByte = 0;
	unsigned int bytesNeeded;
	static R_RANDOM_STRUCT randomStruct;

	R_RandomInit(&randomStruct);

	// Initialize with all zero seed bytes, which will not yield an actual
			 random number output. 

	while (1) {
		R_GetRandomBytesNeeded(&bytesNeeded, &randomStruct);
		if(bytesNeeded == 0)
			break;

		R_RandomUpdate(&randomStruct, &seedByte, 1);
	}

	return(&randomStruct);
}


  void ASCII_to_Hex( char *hex_buf, char *asc_buf, int len )
{
	int	i,leng;
	char	ch;

	leng = len / 2;
	for( i=0; i<leng; i++ )
	{
		ch = asc_buf[2*i];
		if ( ch >= '0' && ch <= '9' )
			ch = ch - '0';
		else
			if ( ch >= 'a' && ch <= 'f' )
				ch = ch - 'a' + 0x0a;
			else
				if ( ch >= 'A' && ch <= 'F' )
					ch = ch - 'A' + 0x0a;
				else
					ch = 0x00;
		hex_buf[i] = ch << 4; 
		ch = asc_buf[2*i+1];
		if ( ch >= '0' && ch <= '9' )
			ch = ch - '0';
		else
			if ( ch >= 'a' && ch <= 'f' )
				ch = ch - 'a' + 0x0a;
			else
				if ( ch >= 'A' && ch <= 'F' )
					ch = ch - 'A' + 0x0a;
				else
					ch = 0x00;
		hex_buf[i] = hex_buf[i] + ch; 
	}
}



void Hex_to_ASCII( char *asc_buf, char *hex_buf, int len )
{
	int	i;
	char	ch;
	
	for( i=0; i<len; i+=2 )
	{
		ch = ( hex_buf[i/2] & 0xf0 ) >> 4;
		if ( ch >= 0x0a && ch <= 0x0f )
			asc_buf[i] = ch + 'A' - 10;
		else
                	asc_buf[i] = ch +'0';
                ch =  hex_buf[i/2] & 0x0f ;
		if ( ch >= 0x0a && ch <= 0x0f )
			asc_buf[i+1] = ch + 'A' - 10;
		else
                	asc_buf[i+1] = ch +'0';
	}
}


void trimoff(char **in)
{
	char *p = *in;
	while (*p == '0')
	{
		p++;
	}
	*in = p;

}

void initPub(R_RSA_PUBLIC_KEY *rsapub,char *pube,char *pubm)
{
	char buf[2048];
	char hbuf[1024];
	strcpy(buf,"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010001");
//	strcpy(buf,pube);
	ASCII_to_Hex(hbuf,buf,256);
	rsapub->bits = 1024;
	memcpy(rsapub->exponent,hbuf,128);
	//strcpy(buf,"6DDAA409C3F941BDF45897201A915C4FE93DAB23158A920D4C641B4E3FC0331570FEB9BEB83BF6FE27D024953FF90EB51AF68CFC1BA18C8C20968CA9B6069A4206137A414F724F5F051B1A15D36EDFFFAD5CB0A959B14EDB2EDADEA189049D34DD9A33E1B586F5F94761CD0EFF377CE1E4A415F3CBCA16E8B75BCE78FBF07701");
	strcpy(buf,pubm);
	ASCII_to_Hex(hbuf,buf,256);
	memcpy(rsapub->modulus,hbuf,128);
}


void initPub1(R_RSA_PUBLIC_KEY *rsapub)
{
	char buf[2048];
	char hbuf[1024];
	strcpy(buf,"4E20002931C6428AE50B5BEEE8F617C07D0FEE75B50C27726DB183F9D23CCE17A1948664AC8178D6A759CFAA95247AE1319BC608455E4EA61946F82279E19832081BB91386080A945A78D28E8C8E3859FED2D4D71F5D320882CEEA810493A261E1387BEF63CC3082B63C89526E5AFE1937D5541571E99CF2DC16922ED531B541");
	ASCII_to_Hex(hbuf,buf,256);
	rsapub->bits = 1024;
	memcpy(rsapub->exponent,hbuf,128);
	strcpy(buf,"6DDAA409C3F941BDF45897201A915C4FE93DAB23158A920D4C641B4E3FC0331570FEB9BEB83BF6FE27D024953FF90EB51AF68CFC1BA18C8C20968CA9B6069A4206137A414F724F5F051B1A15D36EDFFFAD5CB0A959B14EDB2EDADEA189049D34DD9A33E1B586F5F94761CD0EFF377CE1E4A415F3CBCA16E8B75BCE78FBF07701");
	ASCII_to_Hex(hbuf,buf,256);
	memcpy(rsapub->modulus,hbuf,128);
}


int isNum(char *buf)
{
	int len;
	int i;
	if ((buf == NULL) || strlen(buf) == 0)
		return 0;
	len = strlen(buf);
	for(i=0;i<len;i++)
	{
		if (buf[i] < '0' || buf[i] > '9')
			return 0;
	}
	return 1;
}

int isNumLetter(char *buf)
{
	int len;
	int i;
	int Flag = 0;
	if ((buf == NULL) || strlen(buf) == 0)
		return 0;
	len = strlen(buf);
	for(i=0;i<len;i++)
	{
		Flag = ((buf[i] >= '0' && buf[i] <= '9') || (buf[i] >= 'a' && buf[i] <= 'z') || (buf[i] >= 'A' && buf[i] <= 'Z'));
		if (!Flag)
			return 0;
	}
	return 1;
}

int pwd2ans98(char *pwd)
{
	int len = 0;
	char buf[17];
	memset(buf,'F',16);
	buf[16] = '\0';
	if((pwd == NULL) || strlen(pwd) < 4 ||strlen(pwd) > 6)
		return -1;
	len = strlen(pwd);
	sprintf(buf,"%02d",len);
	memcpy(buf+2,pwd,len);
	strcpy(pwd,buf);
	return 0;
}				
 */
